<?php

/*
 * This class is used for as Model for Site table 
 */

class Model_Sitehours extends Model_Base {

    protected $_name = 'tbl_site_hours';
    protected $_primaryKey = 'site_hr_id';

  /**
     * Fetch an individual entry for view
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id) {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('site' => 'tbl_site_hours'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = site.updated_lgn_id")
                ->where('site_hr_id = ?', $id);
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     * Fetch an individual entry 
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('site' => 'tbl_site_hours'))
                ->setIntegrityCheck(false)
                ->where('site_hr_id = ?', $id);

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    public function fetchEntryBysiteId($siteid) {
        
        $finalArray = array();
        $select = $this->select()
                ->from(array('site' => 'tbl_site_hours'))
                ->setIntegrityCheck(false)
                ->where('site_id = ?', $siteid);

        $data = $this->fetchAll($select);
        #echo $data;exit;
        
        foreach ($data as $com) {
            $finalArray[$com['site_hr_day']]['site_hr_id'] = $com['site_hr_id'];
            $finalArray[$com['site_hr_day']]['site_id'] = $com['site_id'];
            $finalArray[$com['site_hr_day']]['site_hr_day'] = $com['site_hr_day'];
            $finalArray[$com['site_hr_day']]['site_hr_commision_hr'] = $com['site_hr_commision_hr'];
            $finalArray[$com['site_hr_day']]['site_hr_commision_mint'] = $com['site_hr_commision_mint'];
            $finalArray[$com['site_hr_day']]['site_hr_decommision_hr'] = $com['site_hr_decommision_hr'];
            $finalArray[$com['site_hr_day']]['site_hr_decommision_mint'] = $com['site_hr_decommision_mint'];
            $finalArray[$com['site_hr_day']]['site_hr_date'] = $com['site_hr_date'];
            $finalArray[$com['site_hr_day']]['updated_lgn_id'] = $com['updated_lgn_id'];
        }
        
        
        if (!empty($data)) {
            return $finalArray;
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get sites
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchSitehours($where = "", $limit = "", $offset = "0", $order = 'site_hr_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('site' => 'tbl_site_hours'))
                ->setIntegrityCheck(false)
                ->join(array('st' => 'site'), "site.site_id = st.site_id");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getSitehoursCount($where) {
        $select = $this->select()
                ->from(array('site' => 'tbl_site_hours'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
               ->join(array('st' => 'site'), "site.site_id = st.site_id");
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete sites
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteSitehours($prdtArray) {

        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_site) {
                $siteData = Array();
                $siteData = $this->fetchEntryById($_site);
                if (count($siteData) > 0) {
                    
                    $this->delete('site_id =' . (int) $_site);
                }
            }
        } else {
            $site_id = $prdtArray;
            $siteData = $this->fetchEntryById($site_id);
            if (count($siteData) > 0) {
                $this->delete('site_id =' . $site_id);
            }
        }
    }
    
    
   
    
    
  
    

}
